package com.vfa.file;

import java.util.ArrayList;
import java.util.List;

import com.vfa.vocaburay.R;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;


public class ListArrayAdapterDialog extends BaseAdapter implements OnClickListener {
	
	private LayoutInflater mInflater;
	
	private int mViewResourceId;
	
	private CheckBox mLastCheck = null;
	
	private int mLastChecked = -1;
	
	
	private List<FileInfo> mFileInfo = new ArrayList<FileInfo>();

	
	public ListArrayAdapterDialog(Context context,int viewResourceId, List<FileInfo> fileInfos){		
		mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mViewResourceId = viewResourceId;
		mFileInfo = fileInfos;
		
	}
	public void reset()
	{
		 mLastCheck = null;
		
		 mLastChecked = -1;
	}
	@Override
	public int getCount() {
		return mFileInfo.size();
	}

	@Override
	public FileInfo getItem(int position) {
		return mFileInfo.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {		
		ViewHolder viewholder;
		if(convertView == null){
			convertView = mInflater.inflate(mViewResourceId, null);
			viewholder = new ViewHolder();
			viewholder.mImage = (ImageView) convertView.findViewById(R.id.img);
			viewholder.mTvFileName = (TextView)convertView.findViewById(R.id.title);
			viewholder.mTvFileCount = (TextView)convertView.findViewById(R.id.detail);
			viewholder.mCbFileChecked = (CheckBox)convertView.findViewById(R.id.checkfile);
			viewholder.mCbFileChecked.setOnClickListener(this);
			convertView.setTag(viewholder);
			
		}else {
			viewholder = (ViewHolder)convertView.getTag();
		}
		
		int pic = mFileInfo.get(position).mFileImageType;
		if (pic == 0) 
			viewholder.mImage.setImageDrawable(mFileInfo.get(position).mFileDrawable);
		else	
			viewholder.mImage.setImageResource(pic);
		viewholder.mTvFileName.setText(mFileInfo.get(position).mFileName);
		viewholder.mTvFileCount.setText(mFileInfo.get(position).mFileCount);
		
		viewholder.mCbFileChecked.setTag(new Integer(position));
		if(mFileInfo.get(position).isChecked){
			viewholder.mCbFileChecked.setChecked(true);
		}else {
			
			viewholder.mCbFileChecked.setChecked(false);
		}
		
		return convertView;
	}
	
	@Override
	public void onClick(View v) {
		final int position = ((Integer)v.getTag()).intValue();		
		if(((CheckBox)v).isChecked()){			
			if(mLastCheck!=null){
				mLastCheck.setChecked(false);
				mFileInfo.get(mLastChecked).isChecked = false;
			}			
			mFileInfo.get(position).isChecked = true;
			mLastCheck = (CheckBox)v;						
			mLastChecked = position;
			
		}else {
			mFileInfo.get(position).isChecked = false;
		}
	}
	
	private class ViewHolder{
		public ImageView mImage;
		public TextView mTvFileName;
		public TextView mTvFileCount;
		public CheckBox mCbFileChecked;
	}
}
